package com.jawmask.android.stuartproair.ui;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.jawmask.android.stuartproair.R;
import com.jawmask.android.stuartproair.constants.UIUtils;

public class SocialNetworkActivity extends BaseListActivity {

	private String[] targetSocialNetworkHttpUrlLinks;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view_layout, true);
		setTitle(getString(R.string.social_title));

		targetSocialNetworkHttpUrlLinks = getResources().getStringArray(R.array.target_social_network_http_url);

		setListAdapter(new ListItemAdapter(getResources().getStringArray(R.array.social_network_actions)));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String selectedSocialNetworkName = (String) getListAdapter().getItem(position);
		String targetSocialNetworkHttpLink = targetSocialNetworkHttpUrlLinks[position];

		if (selectedSocialNetworkName.equals(getString(R.string.facebook))) {
			UIUtils.getInstance().launchWebBrowserScreen(this, targetSocialNetworkHttpLink, selectedSocialNetworkName);
		} else if (selectedSocialNetworkName.equals(getString(R.string.twitter))) {
			UIUtils.getInstance().launchWebBrowserScreen(this, getTwiterUrl(), selectedSocialNetworkName);
		}
	}

	private String getTwiterUrl() {

		try {

			URI requestURL = new URI("https://twitter.com/intent/tweet");

			List<NameValuePair> requestParameter = new ArrayList<NameValuePair>();
			requestParameter.add(new BasicNameValuePair("original_referer", "http://www.stuartproair.com/blog/"));
			requestParameter.add(new BasicNameValuePair("source", "tweetbutton"));
			requestParameter.add(new BasicNameValuePair("text", "Stuart Pro Air Launches New Website"));
			requestParameter.add(new BasicNameValuePair("url", "http://www.stuartproair.com/uncategorized/stuart-pro-air-launches-new-website/"));
			requestParameter.add(new BasicNameValuePair("via", "stuartproair"));

			return URIUtils.createURI(requestURL.getScheme(), requestURL.getAuthority(), -1, requestURL.getPath(),
					URLEncodedUtils.format(requestParameter, HTTP.UTF_8), null).toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}